package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UsermanagerService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ProjectName: health_parent
 * @Package: com.itheima.controller
 * @ClassName: UserManagerController
 * @Author: Torifu
 * @Date: 2019/7/5 11:26
 * @Description:
 * @Version: 1.0
 */

@RestController
@RequestMapping("/usermanager")
public class UserManagerController {

    @Reference
    private UsermanagerService usermanagerService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = usermanagerService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }

    @RequestMapping("/add")
    public Result add(@RequestBody User user,Integer[] roleIds){

        try {
            usermanagerService.add(user,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_USER_SUCCESS);
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){

        try {
            usermanagerService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
    }

    @RequestMapping("/findUserById")
    public Result findUserById(Integer id){

        try {
            User user = usermanagerService.findUserById(id);
            return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_USER_FAIL);
        }
    }

    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id){

        try {
            List<Integer> roleIds = usermanagerService.findRoleIdsByUserId(id);
            return new Result(true,MessageConstant.QUERY_ROLEIDS_SUCCESS,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLEIDS_FAIL);
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody User user,Integer[] roleIds){

        try {
            usermanagerService.edit(user,roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_USER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_USER_SUCCESS);
    }
    
}
